package model.hospital;

import net.passClass.PassData;

import java.util.TreeSet;

public class Building implements Comparable<Building> {
    private int buildingNumber;
    private TreeSet<Floor> floors = new TreeSet<>();

    @Override
    public int compareTo(Building o) {
        return this.buildingNumber - o.buildingNumber;
    }

    public PassData add(Room room) {
        Floor aFloor = new Floor(room.getLocation().getFloorNumber());
        if(room.getLocation().getRoomNumber()==0){       //新增楼层
            if(floors.contains(aFloor)){
                return new PassData(false,null,"楼层已存在，新增失败");
            }
            floors.add(aFloor);
            return new PassData(true,null,"楼层新增成功");
        }
        if(!floors.contains(aFloor)){               //新增房间
            floors.add(aFloor);
        }
        return floors.ceiling(aFloor).add(room);
    }

    public PassData delete(Room room) {
        Floor aFloor = new Floor(room.getLocation().getFloorNumber());
        Floor theFloor = floors.ceiling(aFloor);
        if(!floors.contains(theFloor)){
            return new PassData(false, null,"楼层不存在，删除失败");
        }
        if (room.getLocation().getRoomNumber()==0){     //即删除楼层
            if(!theFloor.getRooms().isEmpty()){
                return new PassData(false,null,"楼层中存在房间，删除失败");
            }
            floors.remove(theFloor);
            return new PassData(true,null,"楼层删除成功");
        }

        return theFloor.delete(room);        //删除楼层内对象

    }

    public Building(int buildingNumber) {
        this.buildingNumber = buildingNumber;
    }

    public Building() {
    }

    public int getBuildingNumber() {
        return buildingNumber;
    }

    public void setBuildingNumber(int buildingNumber) {
        this.buildingNumber = buildingNumber;
    }

    public TreeSet<Floor> getFloors() {
        return floors;
    }

    public void setFloors(TreeSet<Floor> floors) {
        this.floors = floors;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Building building = (Building) o;
        return buildingNumber == building.buildingNumber;
    }
}
